﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using CSharpDesign.DAL;
using CSharpDesign.Scripts;

namespace CSharpDesign
{
    public partial class ChaKanShangPin : System.Web.UI.Page
    {
        SqlHelper data = new SqlHelper();
        SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = " 水果商店";
            if (!IsPostBack)
            {

                data.RunSql("update ShangPin set SPClick=SPClick+1 where SPID=" + Request.QueryString["id"].ToString());
                string sql = "select * from ShangPin where SPID=" + Request.QueryString["id"].ToString();
                getdata(sql);


            }
        }
        private void getdata(string sql)
        {
            SqlDataReader dr = data.GetDataReader(sql);
            if (dr.Read())
            {

                Label2.Text = dr["SPName"].ToString();
                Label4.Text = dr["SPPrice"].ToString();
                Label5.Text = dr["SPNum"].ToString();
                DIV1.InnerHtml = dr["SPIntroduce"].ToString();
                Label6.Text = dr["SPClick"].ToString();
                iGPhoto.ImageUrl = "files/" + dr["SPPhoto"].ToString();
                Label3.Text = dr["ShangPinFenLeiName"].ToString();
                Hidden1.Value = dr["ShangPinFenLeiID"].ToString();

                Label1.Text = dr["SPXingHao"].ToString();
                Label7.Text = dr["SPGuiGe"].ToString();
                Label8.Text = dr["SPDanWei"].ToString();

            }

        }
        protected void btnShop_Click(object sender, EventArgs e)
        {

            if (float.Parse(Label5.Text) < float.Parse(TextBox1.Text))
            {
                Alert.AlertAndRedirect("对不起您购买的商品不能大于库存数量", "ChaKanShangPin.aspx?id=" + Request.QueryString["id"].ToString());

            }
            else
            {
                string Orderid;
                if (Session["UserName"] == null)
                {
                    Alert.AlertAndRedirect("您还没有登录，请登录后再购买，谢谢合作！", "Default.aspx");

                }
                else
                {
                    SqlDataReader dr = data.GetDataReader("select top 1 * from GouWuChe where IsCheckout='否' and  Ordeuser='" + Session["UserName"].ToString() + "' order by id desc  ");
                    if (dr.Read())
                    {
                        Orderid = dr["Orderid"].ToString();
                    }
                    else
                    {

                        Orderid = DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                        string sqlOrder = "insert into GouWuChe(Orderid,Ordeuser,OrderStite)values('" + Orderid + "','" + Session["UserName"].ToString() + "','待付款')";
                        data.RunSql(sqlOrder);
                    }


                    sqlconn.Open();
                    string strid = Page.Request.QueryString["id"];


                    SqlDataReader drDD = data.GetDataReader("select * from  DingDan where  SPID='" + strid + "' and  IsCheckout='否' and OrderMember='" + Session["UserName"].ToString() + "'  and OrderID='" + Orderid + "'  ");

                    if (drDD.Read())
                    {
                        data.RunSql("update  DingDan set shuliang=shuliang+" + TextBox1.Text + "   where  SPID='" + strid + "' and  IsCheckout='否' and OrderMember='" + Session["UserName"].ToString() + "'  and OrderID='" + Orderid + "'   ");
                        Response.Redirect("Shopping.aspx");
                    }
                    else
                    {
                        float Newdanjia = float.Parse(Label4.Text) * (float.Parse(Session["ZheKou"].ToString()) / 100);
                        string sqlstr = "insert into DingDan"
                            + "(OrderID,OrderMember,SPID,SPName,ShangPinFenLeiID,ShangPinFenLeiName,SPPrice,IsCheckout,shuliang)"
                            + " values('" + Orderid + "','" + Session["UserName"].ToString() + "','" + Request.QueryString["id"].ToString() + "','"
                            + Label2.Text + "','" + Hidden1.Value + "','" + Label3.Text
                            + "','" + Newdanjia + "','否','" + TextBox1.Text + "')";
                        data.RunSql(sqlstr);
                        Response.Redirect("Shopping.aspx");
                    }
                }
            }
        }

    }

}